﻿using com.uplus.log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApi.Models;

namespace WebApi.Bill.Biz
{
    public class BizBufferWorkShop : BizBase
    {
        readonly string tb = "TbBufferWorkShop";
        readonly string pk = "pkId";

        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(BufferWorkShopModel model)
        {
            return Da.InsertModel<BufferWorkShopModel>(model);
        }
        #endregion

        #region 更新
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(BufferWorkShopModel model)
        {
            return Da.UpdateModel(model);
        }
        #endregion

        #region GetList
        /// <summary>
        /// 获取信息列表
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataTable GetList(string sql)
        {
            return Da.GetList(sql);
        }

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="pageNum">当前页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="recordCount">总列数</param>
        /// <returns></returns>
        public List<BufferWorkShopModel> GetPageList(dynamic data, ref int recordCount, ref int totalPage)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($"select * from {this.tb} where 1=1");
            JObject joFilters = FiltersSql.GetQueryCond(data);
            sb.Append(joFilters["sqlAppend"].ToString());
            sb.Append($" order by tCreate desc");
            int pageNum = Convert.ToInt32(joFilters["pageNum"]);
            int pageSize = Convert.ToInt32(joFilters["pageSize"]);
            return Da.PageList<BufferWorkShopModel>(sb.ToString(), pageNum, pageSize, ref recordCount, ref totalPage);
        }
        #endregion

        #region Get
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public BufferWorkShopModel Get(int code)
        {
            return Da.GetModel<BufferWorkShopModel>(x => x.Code == code);
        }
        #endregion

        #region Delete 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public int Delete(int code)
        {
            return Da.Delete(this.tb, this.pk, code);
        }
        #endregion

        #region 接受车间数据
        /// <summary>
        /// 接受车间数据 add by cfl  2021-12-03
        /// </summary>
        /// <param name="data"></param>
        public void ReceiveWorkShopData(dynamic data)
        {
            List<BufferWorkShopData> shopDataList = JsonConvert.DeserializeObject<List<BufferWorkShopData>>(JsonConvert.SerializeObject(data));
            foreach (var shopData in shopDataList)
            {
                try
                {
                    BufferWorkShopModel model = new BufferWorkShopModel();
                    model.SRoomName = shopData.RoomName;
                    model.SRoomStatus = shopData.RoomStatus;
                    model.SRoomPrincipal = shopData.RoomPrincipal;
                    model.SRoomMaxMun = shopData.RoomMaxMun;
                    model.SProductName = shopData.ProductName;
                    model.SProductBatch1 = shopData.ProductBatch1;
                    model.SProductBatch2 = shopData.ProductBatch2;
                    model.TDayClean = Convert.ToDateTime(shopData.DayClean);
                    model.TEffectiveCleaningTime = Convert.ToDateTime(shopData.EffectiveCleaningTime);
                    model.TTwLastClean = Convert.ToDateTime(shopData.TwLastClean);
                    model.TTwNextClean = Convert.ToDateTime(shopData.TwNextClean);
                    model.TNmLastClean = Convert.ToDateTime(shopData.NmLastClean);
                    model.TNmNextClean = Convert.ToDateTime(shopData.NmNextClean);
                    Add(model);
                }
                catch (Exception ex)
                {
                    Log4Helper.Error(this.GetType(),$"异常：{ex}");
                    continue;
                }
            }

        }

        #endregion

        #region 获取车间数据
        /// <summary>
        /// 获取车间数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public BufferWorkShopModel GetWorkShopData()
        {
            BufferWorkShopModel model = new BufferWorkShopModel();
            model = Da.GetModelList<BufferWorkShopModel>().OrderByDescending(x => x.TDayClean).FirstOrDefault();
            return model;
        }

        #endregion
    }
}
